<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/18
 * Time: 14:27
 */
header("Content-type:text/html;charset=utf-8");
$arr = [
    [
        'code'=>'000az.sz',
        'name'=>'粤A高速A',
        'netProfit'=>15748.36,
        'amplifaction'=>18.49,
        'grossProfit'=>49.27
    ],
    [
        'code'=>'000az.sz',
        'name'=>'粤A高速B',
        'netProfit'=>1748.36,
        'amplifaction'=>38.49,
        'grossProfit'=>49.27
    ],
    [
        'code'=>'000az.sz',
        'name'=>'粤A高速C',
        'netProfit'=>25748.36,
        'amplifaction'=>28.49,
        'grossProfit'=>49.27
    ],
    [
        'code'=>'000az.sz',
        'name'=>'粤A高速D',
        'netProfit'=>5748.36,
        'amplifaction'=>8.49,
        'grossProfit'=>49.27
    ],
    [
        'code'=>'000az.sz',
        'name'=>'粤A高速',
        'netProfit'=>15748.36,
        'amplifaction'=>0.49,
        'grossProfit'=>49.27
    ]
];
$sortType = $_GET['st'];
if ($sortType==1){
    $tmpArr = [];
    //取一个临时数组，我们要以哪个值进行排序，就取哪个
    foreach($arr as $item){
        $tmpArr[] = $item['netProfit'];
    }
//    var_dump($tmpArr);
    //对数组进行排序，一定要保留原始键
    asort($tmpArr);
//    var_dump($tmpArr);

    $bArr = [];
    //循环排序后的数组，重新生成一个新的数组，用排序后的数组中的键，与原始数组中的键相对应，取原始数组中的值。
    foreach($tmpArr as $k=>$v){
        $bArr[] = $arr[$k];
    }
}elseif($sortType==2){
    $tmpArr = [];
    foreach($arr as $item){
        $tmpArr[] = $item['amplifaction'];
    }
//    var_dump($tmpArr);
    asort($tmpArr);
//    var_dump($tmpArr);
    $bArr = [];
    foreach($tmpArr as $k=>$v){
        $bArr[] = $arr[$k];
    }
}
else {
    $bArr = $arr;
}

?>
<table border="1">
    <tr>
        <th>证券代码</th>
        <th>证券简称</th>
        <th><a href="7stockCode.php?st=1"> 净统利润（万元）</a></th>
        <th><a href="?st=2">业务增减幅</a></th>
        <th>销售毛利率</th>
    </tr>
    <?php
    foreach($bArr as $item) {
        ?>
        <tr>
            <td><?php echo $item['code'] ?></td>
            <td><?php echo $item['name'] ?></td>
            <td><?php echo $item['netProfit'] ?></td>
            <td><?php echo $item['amplifaction'] ?></td>
            <td><?php echo $item['grossProfit'] ?></td>
        </tr>
     <?php   
    }
    ?>
</table>
